package com.bds.wm.action;

import com.bds.wm.model.common.Result;
import com.bds.wm.service.UserService;
import com.bds.wm.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by guoyu on 2018/6/5.
 */
@Controller
public class UserController extends BaseAction {
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(UserController.class);

    @Resource
    private UserService userService;

    @RequestMapping(value = "/service/user/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(HttpServletRequest httpRequest, HttpServletResponse httpResponse, String username,
                        String password) throws IOException {
        Result ret = new Result();
        try {
            ret = userService.login(httpRequest, username, password);
            if (ret.getCode().equals(Const.SUC)) {
                ret.setCode(Const.SUC);
                ret.setData(ret.getData());
                LOGGER.info("[controller]-[2]-用户登录成功-username=" + username);
                // httpResponse.sendRedirect("/pages/face.html");
                return ret;
            } else {
                ret.setCode(Const.FAIL);
                LOGGER.info("[controller]-[3]-用户登录异常-username=" + username);
            }
        } catch (Exception e) {
            LOGGER.warn("[controller]-[33]-用户登录异常-username=" + username, e);
            ret.setResult(Const.FAIL, "用户登录异常");
        }
        return ret;

    }
}
